package com.imau.cookie;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/cookieA")
public class AServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.创建cookie  不支持创建中文值：乱码问题  http 使用URL编码解码
        //解决方案：
        String value = "张三";
        Cookie cookie = new Cookie("name2", URLEncoder.encode(value));
        //cookie存活时间
        //设置正数：将cookie写入浏览器所在电脑的硬盘，持久化存储，到期自动删除
        cookie.setMaxAge(7 * 24 * 60 * 60);

        //2.发送到客户端
        response.addCookie(cookie);

    }
}
